package com.meichis.yndms.adapter.RecycleViewItemType;

import android.app.Activity;
import android.text.*;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meichis.mcsappframework.adapter.recyclerview.base.ItemViewDelegate;
import com.meichis.mcsappframework.adapter.recyclerview.base.ViewHolder;
import com.meichis.mcsappframework.utils.ParseUtil;
import com.meichis.mcsappframework.utils.ScreenUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.model.InspectTemplate_Item;
import com.meichis.yndms.ui.activity.VST_FreeQuestionnaireLogicActivity;

import java.util.List;

/**
 * Created by pei on 2016/8/24.
 * 默认item
 */
public class FillMode_EditText implements ItemViewDelegate<InspectTemplate_Item> {

    private final Activity activity;
    private List<InspectTemplate_Item> items;

    public FillMode_EditText(Activity activity, List<InspectTemplate_Item> items) {
        this.items = items;
        this.activity = activity;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.fill_mode_edit;
    }

    @Override
    public boolean isForViewType(InspectTemplate_Item item, int position) {
        //mode  1,2, 8, >9
        return item.getItem().getFillMode() < 3 || (item.getItem().getFillMode() > 12 && item.getItem().getFillMode() != 22) || item.getItem().getFillMode() == 8;
    }

    @Override
    public void convert(final ViewHolder holder, final InspectTemplate_Item item, int position) {
        TextView tv_group = holder.getView(R.id.tv_group);
        if (position == 0) {
            if (!TextUtils.isEmpty(item.getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                holder.setVisible(R.id.v_group, true);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
                holder.setVisible(R.id.v_group, false);
            }
        } else {
            if (!TextUtils.isEmpty(item.getItemGroup()) && !item.getItemGroup().equals(items.get(position - 1).getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                holder.setVisible(R.id.v_group, true);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
                holder.setVisible(R.id.v_group, false);
            }
        }

        holder.setText(R.id.tv_question, Html.fromHtml(String.valueOf(position + 1) + ". " +
                item.getItem().getName() +
                (!TextUtils.isEmpty(item.getItem().getUnit()) ? "(" + item.getItem().getUnit() + ")" : "") +
                (item.getIsMustFill() == 1 ? " <font  color=\"#FF0000\">*</font>" : "") +
                (!TextUtils.isEmpty(item.getItem().getSubTitle()) ? "<br/> <font color=\"#8D8D8D\" ><small>" + item.getItem().getSubTitle() + "</small></font>" : "")
        ));
        holder.getView(R.id.iv_data).setVisibility(item.getItem().getFillMode() == 7 ? View.VISIBLE : View.GONE);
        final LinearLayout ll = holder.getView(R.id.ll_answer);
        setBackground(item, ll);
        final EditText et_answer = holder.getView(R.id.et_answer);
        int inputType = InputType.TYPE_CLASS_TEXT;
        switch (item.getItem().getFillMode()) {
            case 1:
                inputType = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL;
                break;
            case 8:
                inputType = InputType.TYPE_CLASS_NUMBER;
                break;
            default:
                break;
        }
        et_answer.setInputType(inputType);
        et_answer.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                View nextView = v.focusSearch(View.FOCUS_DOWN);
                if (nextView != null) {
                    nextView.requestFocus(View.FOCUS_DOWN);
                }
                return true;
            }
        });
        if (!holder.haveTextChangedListener(et_answer)) {
            holder.setTextChangedListener(et_answer, new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    InspectTemplate_Item item = items.get(holder.getAdapterPosition());

                    if (item.getItem().getFillMode() == 1 || item.getItem().getFillMode() == 8) {
                        if (!TextUtils.isEmpty(item.getItem().getMinValue()) && ParseUtil.parseDouble(s) < ParseUtil.parseDouble(item.getItem().getMinValue())) {
                            et_answer.setError("最小值为：" + item.getItem().getMinValue());
                            return;
                        }
                        if (!TextUtils.isEmpty(item.getItem().getMaxValue()) && ParseUtil.parseDouble(s) > ParseUtil.parseDouble(item.getItem().getMaxValue())) {
                            et_answer.setError("最大值为：" + item.getItem().getMaxValue());
                            return;
                        }
                    } else {
                        if (!TextUtils.isEmpty(item.getItem().getRegularExpression()) && !s.toString().matches(item.getItem().getRegularExpression())) {
                            et_answer.setError("最大值为：" + "确保文本格式为：" + item.getItem().getRegularExpression());
                            return;
                        }
                    }
                    item.setFillName(s.toString());
                    item.setFillValue(s.toString());
                    setBackground(item, ll);
                    ((VST_FreeQuestionnaireLogicActivity) activity).notifyAdapter();

                }
            });
        }

        et_answer.setText(item.getFillName());
        et_answer.setMinHeight(ScreenUtil.dip2px(item.getItem().getFillMode() == 2 ? 80 : 40));
    }

    private void setBackground(InspectTemplate_Item item, LinearLayout ll) {
        ll.setBackgroundResource(item.getFillName().trim().length() == 0 && item.getIsMustFill() == 1 ? R.drawable.bg_ll_frame : R.drawable.bg_ll_frame);
    }
}
